Skip to content

gh-108951: document addition of TaskGroup.cancel()#149031

Open
belm0 wants to merge 2 commits intopython:mainfrom
belm0:taskgroup_cancel_docs
Open

gh-108951: document addition of TaskGroup.cancel()#149031
belm0 wants to merge 2 commits intopython:mainfrom
belm0:taskgroup_cancel_docs

Conversation

@belm0
Copy link
Copy Markdown
Contributor

@belm0 belm0 commented Apr 26, 2026

@@ -1 +1 @@
Add :meth:`~asyncio.TaskGroup.cancel` which cancels unfinished tasks and exits the group without error.
Add :meth:`~asyncio.TaskGroup.cancel` which cancels unfinished tasks and exits the group without raising :exc:`asyncio.CancelledError`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not sure if the explicit "and exits the group without raising :exc:asyncio.CancelledError" makes sense. All this does is 1) cancel all current tasks in the group and 2) set the task group into aborted mode which causes creation of further tasks to fail. With that in mind, I'm not sure why you chose to explicitly mention exiting the task group.

Copy link
Copy Markdown
Contributor

@agronholm agronholm Apr 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further, this still begs to question why you said "without raising :exc:asyncio.CancelledError". This implies that otherwise it would raise a CancelledError, yes?

Copy link
Copy Markdown
Contributor Author

@belm0 belm0 Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, if you check the long discussions in the impl PR, there was quite a bit made about possible confusion from expecting TaskGroup.cancel() to behave like Task.cancel(). So this is being explicit that unlike the latter, TaskGroup.cancel() does not cause anything to raise CancelledError.

Comment thread Doc/whatsnew/3.15.rst
Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
Comment thread Doc/whatsnew/3.15.rst
Comment on lines 718 to 729
(Contributed by Stan Ulbrych in :gh:`148981`.)

asyncio
-------

* Added :meth:`~asyncio.TaskGroup.cancel` to allow early termination of a task group --
e.g. when the goal of the tasks has been achieved or their services no longer needed.
Previously this would involve unintuitive boilerplate such as an extra task raising
a custom exception which is then suppressed as it exits TaskGroup.
(Contributed by John Belmonte in :gh:`127214`.)

base64
Copy link
Copy Markdown
Member

@picnixz picnixz Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(Contributed by Stan Ulbrych in :gh:`148981`.)
asyncio
-------
* Added :meth:`~asyncio.TaskGroup.cancel` to allow early termination of a task group --
e.g. when the goal of the tasks has been achieved or their services no longer needed.
Previously this would involve unintuitive boilerplate such as an extra task raising
a custom exception which is then suppressed as it exits TaskGroup.
(Contributed by John Belmonte in :gh:`127214`.)
base64
(Contributed by Stan Ulbrych in :gh:`148981`.)
asyncio
-------
* Added :meth:`~asyncio.TaskGroup.cancel` to allow early termination of a task,
for instance, when the goal of the tasks has been achieved or their services
no longer needed. Previously this would involve unintuitive boilerplate such
as an extra task raising a custom exception which is then suppressed as it
exits the task group.
(Contributed by John Belmonte in :gh:`127214`.)
base64

@picnixz picnixz changed the title gh-108951: Whatsnew and doc tweaks for TaskGroup.cancel() gh-108951: document addition of TaskGroup.cancel() Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants